package com.leyou.item.service.impl;

import com.leyou.item.mapper.SpecGroupMapper;
import com.leyou.item.mapper.SpecParamMapper;
import com.leyou.item.pojo.SpecGroup;
import com.leyou.item.pojo.SpecParam;
import com.leyou.item.service.SpecService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SpecServiceImpl implements SpecService {

    @Autowired
    private SpecGroupMapper specGroupMapper;

    @Autowired
    private SpecParamMapper specParamMapper;


    @Override
    public List<SpecGroup> querySpecGroups(Long cid) {
        SpecGroup specGroup = new SpecGroup();
        specGroup.setCid(cid);
        List<SpecGroup> specGroups = this.specGroupMapper.select(specGroup);
        specGroups.forEach(specGroup1 -> {
            SpecParam specParam = new SpecParam();
            specParam.setCid(cid);
            specParam.setGroupId(specGroup1.getId());
            specGroup1.setSpecParams(this.specParamMapper.select(specParam));
        });
        return specGroups;
    }

    @Override
    public List<SpecParam> querySpecParam(Long gid, Long cid, Boolean searching, Boolean generic) {
        SpecParam specParam = new SpecParam();
        specParam.setGroupId(gid);
        specParam.setCid(cid);
        specParam.setSearching(searching);
        specParam.setGeneric(generic);
        return this.specParamMapper.select(specParam);
    }

    @Override
    public void insertGroup(SpecGroup group) {
        this.specGroupMapper.insert(group);
    }

    @Override
    public void updateGroup(SpecGroup group) {
        this.specGroupMapper.updateByPrimaryKeySelective(group);
    }
}
